package com.amazon.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.amazon.service.UserService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.amazon.domain.Visiter;
import com.amazon.service.VisiterService;
import com.amazon.mapper.VisiterMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
* @author ext.yangming13
* @description 针对表【t_visiter】的数据库操作Service实现
* @createDate 2025-06-19 13:59:48
*/
@Service
@Slf4j
public class VisiterServiceImpl extends ServiceImpl<VisiterMapper, Visiter>
    implements VisiterService{

    @Resource
    private UserService userService;

    @Override
    public void addVisiter(Visiter param, HttpServletRequest request) {
        String referer = request.getHeader("referer");
        String origin = request.getHeader("origin");
        Cookie[] cookies = request.getCookies();
        if(StringUtils.isNotBlank(referer)){
            param.setIpAddress(referer);
        }else{
            param.setIpAddress(origin);
        }
        param.setCookie_str(JSON.toJSONString(cookies));
        this.save(param);
    }

    @Override
    public JSONObject countInfo() {
        long userNum = userService.count();
        long visiterNum = this.count();
        JSONObject res = new JSONObject();
        res.put("userNum",userNum);
        res.put("visiterNum",visiterNum);
        return res;
    }
}




